package org.snail.activemq.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.log4j.Log4j;
import org.snail.activemq.PushService;
import org.snail.attendence.bean.AttendenceMongo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

@Service
@Log4j
public class AttendencePushServiceImpl implements PushService {

    @Autowired
    private JmsTemplate jmsTemplate;
    @Autowired
    @Qualifier("attendenceMongoQueue")
    private Destination destination;

    @Override
    public void push(Object object) {
        pushExecutor.execute(new Runnable() {
            @Override
            public void run() {
                jmsTemplate.send(destination, new MessageCreator() {
                    @Override
                    public Message createMessage(Session session) throws JMSException {
                        AttendenceMongo attendenceMongo = (AttendenceMongo) object;
                        return session.createTextMessage(JSON.toJSONString(attendenceMongo));
                    }
                });

            }
        });


    }
}
